package cn.iocoder.yudao.module.eval.service.condition;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.eval.controller.admin.condition.vo.EvalApplicationConditionPageReqVO;
import cn.iocoder.yudao.module.eval.controller.admin.condition.vo.EvalApplicationConditionSaveReqVO;
import cn.iocoder.yudao.module.eval.dal.dataobject.condition.EvalApplicationConditionDO;
import cn.iocoder.yudao.module.eval.dal.mysql.condition.EvalApplicationConditionMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.eval.enums.ErrorCodeConstants.*;

/**
 * 申报条件配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class EvalApplicationConditionServiceImpl implements EvalApplicationConditionService {

    @Resource
    private EvalApplicationConditionMapper applicationConditionMapper;

    @Override
    public Long createApplicationCondition(EvalApplicationConditionSaveReqVO createReqVO) {
        // 插入
        EvalApplicationConditionDO applicationCondition = BeanUtils.toBean(createReqVO, EvalApplicationConditionDO.class);
        applicationConditionMapper.insert(applicationCondition);
        // 返回
        return applicationCondition.getId();
    }

    @Override
    public void updateApplicationCondition(EvalApplicationConditionSaveReqVO updateReqVO) {
        // 校验存在
        validateApplicationConditionExists(updateReqVO.getId());
        // 更新
        EvalApplicationConditionDO updateObj = BeanUtils.toBean(updateReqVO, EvalApplicationConditionDO.class);
        applicationConditionMapper.updateById(updateObj);
    }

    @Override
    public void deleteApplicationCondition(Long id) {
        // 校验存在
        validateApplicationConditionExists(id);
        // 删除
        applicationConditionMapper.deleteById(id);
    }

    private void validateApplicationConditionExists(Long id) {
        if (applicationConditionMapper.selectById(id) == null) {
            throw exception(APPLICATION_CONDITION_NOT_EXISTS);
        }
    }

    @Override
    public EvalApplicationConditionDO getApplicationCondition(Long id) {
        return applicationConditionMapper.selectById(id);
    }

    @Override
    public PageResult<EvalApplicationConditionDO> getApplicationConditionPage(EvalApplicationConditionPageReqVO pageReqVO) {
        return applicationConditionMapper.selectPage(pageReqVO);
    }

    @Override
    public List<EvalApplicationConditionDO> getApplicationConditionList(EvalApplicationConditionPageReqVO exportReqVO) {
        return applicationConditionMapper.selectList(exportReqVO);
    }

    @Override
    public List<EvalApplicationConditionDO> getApplicationConditionListByProfessionAndLevel(String professionCode, String skillLevel) {
        return applicationConditionMapper.selectListByProfessionAndLevel(professionCode, skillLevel);
    }

    @Override
    public List<EvalApplicationConditionDO> getApplicationConditionListByProfessionAndMethod(String professionCode, String evaluationMethod) {
        return applicationConditionMapper.selectListByProfessionAndMethod(professionCode, evaluationMethod);
    }

    @Override
    public void toggleApplicationConditionStatus(Long id, Boolean isActive) {
        // 校验存在
        validateApplicationConditionExists(id);

        // 更新状态
        EvalApplicationConditionDO updateObj = new EvalApplicationConditionDO();
        updateObj.setId(id);
        updateObj.setConditionStatus(isActive ? 1 : 0);
        applicationConditionMapper.updateById(updateObj);
    }

    @Override
    public void batchCreateApplicationConditions(List<EvalApplicationConditionSaveReqVO> conditions) {
        for (EvalApplicationConditionSaveReqVO condition : conditions) {
            createApplicationCondition(condition);
        }
    }

}
